package com.madi.company.ui.main.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.aspsine.irecyclerview.animation.ScaleInAnimation;
import com.aspsine.irecyclerview.widget.LoadMoreFooterView;
import com.jaydenxiao.common.base.BaseActivity;
import com.madi.company.R;
import com.madi.company.bean.SearchResumeEntity;
import com.madi.company.contract.SearchResumeContract;
import com.madi.company.model.SearchResumeModel;
import com.madi.company.presenter.SearchResumePresenter;
import com.madi.company.ui.main.adapter.SearchResumeAdapter;
import com.madi.company.utils.MapHelper;

import java.util.List;
import java.util.Map;

import butterknife.Bind;

import static com.madi.company.R.id.irc;

/**
 * 首页搜索简历
 */
public class SearchResumeActivity extends BaseActivity<SearchResumePresenter, SearchResumeModel> implements SearchResumeContract.View, OnRefreshListener, OnLoadMoreListener {

    @Bind(R.id.backBtn)
    LinearLayout mBackBtn;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.textOne)
    TextView mTextOne;
    @Bind(irc)
    IRecyclerView mListView;
    @Bind(R.id.act_search_resume)
    LinearLayout mActSearchResume;
    private SearchResumeAdapter mSearchResumeAdapter;
    private String modelString = "";
    // 关键字搜索 and 简易搜索
    private String keyWord = "", jobType = "", tradeType = "", city = "", userType = "", property = "", education = "", language = "", unsuited = "",
            salaryBegin = "", salaryEnd = "", arrivalTimeBegin = "", arrivalTimeEnd = "", expTimeBegin = "", expTimeEnd = "", workHistory = "", introduction = "";
    private int page = 1;// 页数索引
    private int pageNum = 10;// 每页显示数量
    private boolean isLoadMore = false, isRefesh = false;
    private List<SearchResumeEntity.RowsBean> mRowsBeen;

    @Override
    public int getLayoutId() {
        return R.layout.act_search_resume;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        mTitle.setText(R.string.resume_list);
        mListView.setLayoutManager(new LinearLayoutManager(this));
        mSearchResumeAdapter = new SearchResumeAdapter(this);
        mSearchResumeAdapter.openLoadAnimation(new ScaleInAnimation());
        mListView.setAdapter(mSearchResumeAdapter);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadMoreListener(this);
        loadData("");
    }

    private void loadData(String str) {
        Map<String, String> map = MapHelper.getAccessMap();
        map.put("keyword", keyWord);
        map.put("jobType", jobType);
        map.put("tradeType", tradeType);
        map.put("city", city);
        map.put("userType", userType);
        map.put("property", property);
        map.put("education", education);
        map.put("language", language);
        map.put("unsuited", unsuited);
        map.put("salaryBegin", salaryBegin);
        map.put("salaryEnd", salaryEnd);
        map.put("arrivalTimeBegin", arrivalTimeBegin);
        map.put("arrivalTimeEnd", arrivalTimeEnd);
        map.put("expTimeBegin", expTimeBegin);
        map.put("expTimeEnd", expTimeEnd);
        map.put("workHistory", workHistory);
        map.put("introduction", introduction);
        map.put("pageNum", page + "");
        map.put("rowCountOfOnePage", pageNum + "");
        map.put("condition", modelString);
        mPresenter.getData(map,str );
    }

    //下拉刷新
    @Override
    public void onRefresh() {
        page = 1;
        mListView.setRefreshing(true);
        loadData("1");
    }

    @Override
    public void onLoadMore(View loadMoreView) {
        page++;
        loadData("2");
        mListView.setLoadMoreStatus(LoadMoreFooterView.Status.LOADING);
    }

    @Override
    public void returnResumeData(SearchResumeEntity entity) {
        mRowsBeen = entity.getRows();
        mSearchResumeAdapter.setData(mRowsBeen);
        mSearchResumeAdapter.notifyDataSetChanged();

    }

    @Override
    public void returnLoadMoreResumeData(SearchResumeEntity entity) {
        List<SearchResumeEntity.RowsBean> mlist = entity.getRows();
        if (mlist.size()>0){
            mListView.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
            mRowsBeen.addAll(mRowsBeen.size(),mlist);
            mSearchResumeAdapter.setData(mRowsBeen);
            mSearchResumeAdapter.notifyDataSetChanged();
        }else
            mListView.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);

    }

    @Override
    public void returnRefreshResumeData(SearchResumeEntity entity) {
        mRowsBeen.clear();
        mRowsBeen = entity.getRows();
        mListView.setRefreshing(false);
        mSearchResumeAdapter.setData(mRowsBeen);
        mSearchResumeAdapter.notifyDataSetChanged();
    }

    @Override
    public void showLoading(String title) {
    }

    @Override
    public void stopLoading() {
    }

    @Override
    public void showErrorTip(String msg) {
    }


}
